<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/24
 * Time: 14:49
 */
namespace app\admin\controller;
use app\admin\model\DiGui;
use app\admin\model\Subject;
use think\Controller;
use think\Db;
use think\Request;

class Lianbiao extends Controller
{
    public function show_list(Request $request){
//        $data = $request->param();
        $model_id = $request->param('model_id');
        $obj = new Subject();
        $obj->setModelId($model_id);
        $field = $obj->getField();
        $newfield = [];
        foreach ($field as $values){
            if($values['field']=='name'){
                $newfield['name'] = $values['field_name'];
            }
            if($values['field']=='tubiao'){
                $newfield['tubiao'] = $values['field_name'];
            }
        }
        $data =  NewBuilder::make('table')
            ->addTableHeaderAllDel('lianbiao/del?model_id='.$model_id,'全部删除')
            ->addTableHeaderAdd('lianbiao/show_add?model_id='.$model_id)
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text',$newfield['name'],null,null,null,null,'left')
            ->addTableColumn('pid','text','上级')
            ->addTableColumn('tubiao','img',$newfield['tubiao'],null,null,150,'max-height: 40px');
        foreach ($field as $value){
            if(!($value['field']=='id'||$value['field']=='name'||$value['field']=='pid'||$value['field']=='tubiao'||$value['field']=='status'||$value['field']=='delete')){
                if($value['form_type']=="单图片"){
                    $data->addTableColumn($value['field'],'img',$value['field_name']);
                }elseif ($value['form_type']=="多图片"){
                    $data->addTableColumn($value['field'],'imgs',$value['field_name']);
                }elseif ($value['form_type']=="status"){
                    $data->addTableColumn($value['field'],'status',$value['field_name'],['可','不可']);
                } else{
                    $data->addTableColumn($value['field'],'text',$value['field_name']);
                }
            }
        }
        $data = $data->addTableColumn('delete','status','是否可删除',['可','否'])
            ->addTableColumn('status','switch','状态',['开启','禁用'],'change?model_id='.$model_id)
            ->addTablePage('lianbiao/page?model_id='.$model_id)
            ->addTableRightHeader('操作',null,null,230)
            ->addTableRight('detail','lianbiao/detail?model_id='.$model_id,'查看')
            ->addTableRight('edit','lianbiao/show_add?model_id='.$model_id,'添加')
            ->addTableRight('edit','lianbiao/show_edit?model_id='.$model_id,'修改')
            ->addTableRight('del','lianbiao/del?model_id='.$model_id,'删除')
            ->fetch();
        return $data;
    }
    public function change(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id');
        $obj = new Subject();
        $model = $obj->setModelId($model_id)->getModel();
        $data = Db::name($model['model_table'])->where('id',$id)->find();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Db::name($model['model_table'])->where('id',$id)->update(['status'=>$data['status']])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function page(Request $request){
        $model_id = $request->param('model_id');
        $limit = $request->param('limit',10);
        $page = $request->param('page',1);
        $obj = new Subject();
        $obj->setModelId($model_id);
        $model = $obj->getModel();
        $obj  = new DiGui();
        $shuju = $obj->getData($model['model_table']);
        if($shuju){
            $data['total'] =count($shuju);
            $shuju  = array_slice($shuju,($page-1)*$limit,$limit);
            $data['data'] = $shuju;
        }else{
            $data['total'] = 0;
            $data['data'] = [];
        }
        foreach ($data['data'] as $key=>$value){
            if($value['pid']==0){
                $value['pid'] = '最上级';
            }else{
                $s = Db::name($model['model_table'])
                    ->where('id',$value['pid'])
                    ->find();
                if($s){
                    $value['pid'] = $s['name'];
                }
            }
            if($value['tubiao']){
                $value['tubiao'] = getFilePath($value['tubiao']);
            }
            if($value['cheng']>0){
                $value['name'] = str_repeat('--',$value['cheng']).'|'.$value['name'];
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**加载添加数据
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_add(Request $request){
        $model_id = $request->param('model_id');
        $pid = $request->param('id',null);
        $obj = new Subject();
        return $obj->getLianBiaoForm($model_id,$pid);
    }

    /**添加数据
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        $obj = new Subject();
        if($obj->addLianBiao($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**加载编辑
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_edit(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id');
        $obj = new Subject();
        return $obj->getLianBiaoForm($model_id,null,$id,'edit');
    }

    public function detail(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id');
        $obj = new Subject();
        return $obj->getLianBiaoForm($model_id,null,$id,'edit',false);
    }

    /**修改
     * @param Request $request
     * @return \think\response\Redirect
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit(Request $request){
        $data = $request->param();
        $obj = new Subject();
        if($obj->addLianBiao($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除联表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $id = $request->param('id');
        $model_id = $request->param('model_id');
        $obj = new Subject();
        $obj->setModelId($model_id);
        if(Db::name($obj->getModel()['model_table'])
            ->where('id','in',$id)
            ->update([
                'delete_time'=>1
            ])){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }

    }
}